<?php

namespace Frieza\Modules\BasicCheck\Actions;

use Doraemon\service\BasicCheck\CheckBackgroundInfo;
use Doraemon\service\BasicCheck\CheckCommonInfo;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class BackgroundApproved extends RowAction
{
    public $name = '审核通过';
    const TOAST_ALERT = '你确定要审核通过吗？';

    public function handle($id , Request $request)
    {
        $adminId = Admin::user()->id;

        $data = [
            'check_time' => time(),
            'admin_id' => $adminId,
            'check_status' =>CheckCommonInfo::CHECK_STATUS_YES,
        ];
        $invalidData = [
            'check_time' => time(),
            'admin_id' => $adminId,
            'check_status' =>CheckCommonInfo::CHECK_STATUS_INVALID,
        ];
        $res = false;
        $info = CheckBackgroundInfo::getInfo(['id' => $id]);
        if (!empty($info)) {
            $res = CheckBackgroundInfo::updateInfo(['id' => $id],$data);
            //把该uid下的别的数据置为无效
            CheckBackgroundInfo::updateInfo(['uid' => $info['uid'], 'check_status' => 0, "id[!]" => $id],$invalidData);
        }
        if ($res) {
            return $this->response()->success('审核成功')->refresh();
        } else {
            return $this->response()->error('操作失败,稍后重试!');
        }


    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }

}